PREVIEW: Before we get started making our very first program, let's look at an example.

  • This program creates a ball and net, then draws a line between them.
  • You are asked to estimate the slope of the line.
  • If your guess is close, score! If not....watch where the ball goes! The actual slope will be displayed onscreen.
  • Click Run to try again, and Submit and Next to continue.

To navigate the page using the TAB key, first press ESC to exit the code editor.

#Set the grid and background stage.set_axis(100) stage.create_grid_overlay(10, "grey") stage.set_background("soccerfield") #Create coordinates for ball, create ball ballx = -90 bally = random.randint(-100, 100) ball = codesters.Sprite("soccerball", ballx, bally) #Create coordinates for net, create net netx = 80 nety = random.randint(-100, 100) net = codesters.Sprite("soccernet", 80,nety) net.flip_right_left() #Draw line between ball and net line = codesters.Line(-90, bally, 80, nety, "blue") slopeguess = float(line.ask("What is the slope of the line?")) #Calculate slope rise = nety-bally run = netx-ballx slope = rise/run #Display slope # sprite = codesters.Text("text", x, y) sprite = codesters.Text(slope, 0, 30) #Calculate new point for ball newrise = (slopeguess*run) endy = bally + newrise ball.glide_to(netx, endy)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)